package com.htx.zqs.blesmartmask.bean;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.htx.zqs.blesmartmask.MyApplication;
import com.htx.zqs.blesmartmask.db.ManagerFactory;
import com.htx.zqs.blesmartmask.utils.LocationUtil;
import com.htx.zqs.blesmartmask.utils.Utils;
import com.htx.zqs.greendao.gen.BeforeCourseDataDao;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;

import java.util.List;
import java.util.Properties;

@Entity
public class BeforeCourseData implements Comparable<BeforeCourseData>{
    // 数据库主键，autoincrement设置自增，只能为 long/ Long 类型
    @Id(autoincrement = true)
    private Long sId;
    private int id;
    private String name;
    private String coursedate;
    private String deviceid;
    private String beforedate;
    private String address;
    private String islogo;
    private int beforefwater;
    private int beforeffat;
    private String isUpNet; //是否这条数据上传网络


    public BeforeCourseData(String beforedate, int beforefwater, int beforeffat) {
        this.beforedate = beforedate;
        this.beforefwater = beforefwater;
        this.beforeffat = beforeffat;
        this.islogo = "A";
        this.name = MyApplication.selectCourse.getName();
        this.coursedate = MyApplication.selectCourse.getCoursedate();
        this.deviceid = LocationUtil.getUniquePsuedoID();
        this.isUpNet = "0";
        this.address = Utils.getCurMac();
    }


    @Generated(hash = 1263223425)
    public BeforeCourseData(Long sId, int id, String name, String coursedate, String deviceid,
            String beforedate, String address, String islogo, int beforefwater, int beforeffat,
            String isUpNet) {
        this.sId = sId;
        this.id = id;
        this.name = name;
        this.coursedate = coursedate;
        this.deviceid = deviceid;
        this.beforedate = beforedate;
        this.address = address;
        this.islogo = islogo;
        this.beforefwater = beforefwater;
        this.beforeffat = beforeffat;
        this.isUpNet = isUpNet;
    }


    @Generated(hash = 1186867971)
    public BeforeCourseData() {
    }


    public Long getSId() {
        return this.sId;
    }

    public void setSId(Long sId) {
        this.sId = sId;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCoursedate() {
        return this.coursedate;
    }

    public void setCoursedate(String coursedate) {
        this.coursedate = coursedate;
    }

    public String getDeviceid() {
        return this.deviceid;
    }

    public void setDeviceid(String deviceid) {
        this.deviceid = deviceid;
    }

    public String getBeforedate() {
        return this.beforedate;
    }

    public void setBeforedate(String beforedate) {
        this.beforedate = beforedate;
    }

    public String getIslogo() {
        return this.islogo;
    }

    public void setIslogo(String islogo) {
        this.islogo = islogo;
    }

    public int getBeforefwater() {
        return this.beforefwater;
    }

    public void setBeforefwater(int beforefwater) {
        this.beforefwater = beforefwater;
    }

    public int getBeforeffat() {
        return this.beforeffat;
    }

    public void setBeforeffat(int beforeffat) {
        this.beforeffat = beforeffat;
    }

    public String getIsUpNet() {
        return this.isUpNet;
    }

    public void setIsUpNet(String isUpNet) {
        this.isUpNet = isUpNet;
    }

    public static List<BeforeCourseData> findAll() {
        return ManagerFactory.getInstance().getBeforeCourseManager()
                .queryAll();
    }

    //获取离线数据 油水测试除外
    public static List<BeforeCourseData> findOfflineData(String courseDate) {
        return ManagerFactory.getInstance()
                .getBeforeCourseManager()
                .queryBuilder()
                .where(BeforeCourseDataDao.Properties.Coursedate.eq(courseDate))
                .list();
    }

    public static List<BeforeCourseData> findAllNoUp() {
        return ManagerFactory.getInstance()
                .getBeforeCourseManager()
                .queryBuilder()
                .where(BeforeCourseDataDao.Properties.IsUpNet.eq("0"))
                .list();
    }

    public static void updateUpState() {
        List<BeforeCourseData> list = findAllNoUp();
        for (BeforeCourseData beforeCourseData : list) {
            beforeCourseData.setIsUpNet("1");
        }
        ManagerFactory.getInstance().getBeforeCourseManager()
                .update(list);
    }

    public static void save(List<BeforeCourseData> list) {
        ManagerFactory.getInstance().getBeforeCourseManager().save(list);
    }

    public static void save(BeforeCourseData beforeCourseData) {
        for (BeforeCourseData data : findAll()) {
            if (TextUtils.equals(data.getBeforedate(), beforeCourseData.getBeforedate()) &&
                    TextUtils.equals(data.getCoursedate(), beforeCourseData.getCoursedate()))
                return;
        }
        ManagerFactory.getInstance().getBeforeCourseManager().save(beforeCourseData);
    }


    public String getAddress() {
        return this.address;
    }


    public void setAddress(String address) {
        this.address = address;
    }


    public int getId() {
        return this.id;
    }


    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int compareTo(@NonNull BeforeCourseData o) {
        return o.id - id;
    }
}
